package com.paopao.employee.service;

import com.github.pagehelper.PageHelper;
import com.paopao.common.ServiceHandler;
import com.paopao.domain.EmployeeBean;
import com.paopao.domain.ObjectListResult;
import com.paopao.domain.R;
import com.paopao.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ServiceHandler serviceHandler;

    public R addEmployee(EmployeeBean employeeBean) {
        Integer res = employeeMapper.addEmployee(employeeBean);
        if (res > 0) {
            return new R(200, "OK");
        }else {
            return new R(-1, "error");
        }
    }

    public R deleteEmployee(Integer id) {
        Integer res = employeeMapper.deleteEmployee(id);
        if (res > 0) {
            return new R(200, "OK");
        }else {
            return new R(-1, "error");
        }
    }

    public R editEmployee(EmployeeBean employeeBean) {

        Integer res = employeeMapper.editEmployee(employeeBean);
        if (res > 0) {
            return new R(200, "OK");
        }else {
            return new R(-1, "error");
        }
    }

    public R selectEmployee(int id) {

        EmployeeBean employeeBean = employeeMapper.selectEmployeeByid(id);
        if (employeeBean != null){
            return new R(200, "OK", employeeBean);
        }else {
            return new R(-1, "error");
        }
    }

    public R selectEmployeeList(int pageNum, int pageSize, EmployeeBean employeeBean) {
        PageHelper.startPage(pageNum, pageSize);

        List<EmployeeBean> list = employeeMapper.getList(employeeBean);

        ObjectListResult listResult = serviceHandler.paging(list);

        if (listResult != null && listResult.getList().size() > 0){
            return new R(200, "OK", listResult);
        }else{
            return new R(200, "OK");
        }
    }
}
